package com.markerhub.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.stereotype.Component;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lyz
 * @since 2021/10/8 19:04
 */
public class ReadFileUtil {

    private static final String path = "C:\\ftpdata";

    private static final String fileName = "\\Hotpoint_0_灵瞳B_";

    public static ArrayList<String> getFileData() {
        long l = System.currentTimeMillis();
        long datetime  = l-1000*60*2;

        Date date = new Date(datetime);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
        String format = dateFormat.format(date);
        String time = String.valueOf(Long.valueOf(format));
        ArrayList<String> list = new ArrayList<>();
        try {
            for (int i = 5; i < 10; i++) {
                if (i == 8) {
                    continue;
                }
                File file = new File(path + fileName + i + "_" + time + ".hp");
                if (!file.exists()) {
                    System.out.println("--------------- 暂未读取到设备发送文件数据，请检查如下目录文件是否存在 ! ----------------");
                    System.out.println(new Date() + path + fileName + i + "_" + time + ".hp");
                    continue;
                }
                BufferedReader br = new BufferedReader(
                        new FileReader(file));
                //构造一个BufferedReader类来读取文件
                String s = null;
                while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                    if(StringUtils.isNotBlank(s)){
                        System.out.println(s);
                        list.add(s);
                        System.out.println("获取到的数据---"+s.toString());
                    }
                }
                br.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
